﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace MapSurface.WidgetSurface
{
    public class ReplaceButton: SurfaceButton
    {
        public Label myLabel;
        private object instance;
        public   delegate int ModelHandler(ref object root, object newIntance,object instance,bool all);
        private ModelHandler _myHandler=null;
        public   delegate void SelectElement(object sender) ;
        public SelectElement selectUIElement=null;
        private object selected= null;
        public object rootUI = null;
        private Grid parent;
        public ReplaceButton(string name, Grid parent,object instance, ModelHandler handler, SelectElement selectHandler, object selectUI)
        {
            base.Content = name;
            this.instance = instance;
            base.Click += new System.Windows.RoutedEventHandler(onClick_Handler);
            _myHandler += handler;
            selectUIElement += new SelectElement(select);
            selectUIElement += selectHandler;
            selected = selectUI;
            this.parent = parent;
        }

        void onClick_Handler(object sender, System.Windows.RoutedEventArgs e)
        {
            if (selected != null)
            {
                object newInstance =instance;
                int row = Grid.GetRow((System.Windows.UIElement)instance);
                parent.Children.Remove((System.Windows.UIElement)instance);
                parent = (Grid)removeVisualChid(parent, ref instance);
                parent.Children.Remove(myLabel);
                SurfaceCheckBox scb = (SurfaceCheckBox)parent.Resources["All" + row];
                bool all=false;
                if (scb != null && scb.IsChecked.Value)
                    all = scb.IsChecked.Value;
                newInstance = instance;
                if (_myHandler(ref rootUI, newInstance, selected,all)> -1)
                {
                    
                   
                    RowDefinition rdd = parent.RowDefinitions.ElementAt(row);
                    Label l = new Label();
                    l.Content = selected.GetType().Name;
                    Grid.SetColumn(l, 0);
                    Grid.SetRow(l, row);
                    Grid.SetRow((System.Windows.UIElement)selected, row);
                    Grid.SetColumn((System.Windows.UIElement)selected, 1);
                    if (((FrameworkElement)selected).Parent != null)
                    {
                        object par = ((FrameworkElement)selected).Parent;
                        try
                        {
                            ((Panel)par).Children.Remove((System.Windows.UIElement)selected);
                            parent.Children.Add((System.Windows.UIElement)selected);
                            parent.Children.Add(l);

                        }
                        catch (InvalidCastException)
                        {

                        }
                    }
                    this.myLabel = l;
                    selectUIElement(newInstance);
                    this.instance = selected;
                }

                
                
            }
        }
        private void select(object sender)
        {
            selected = sender;
        }
        private Object removeVisualChid( Object element,ref  Object child)
        {
            if (element != null)
            {
                try
                {
                    Type t = element.GetType();
                    System.Reflection.MethodInfo mi = t.GetMethod("RemoveLogicalChild", BindingFlags.Instance | BindingFlags.NonPublic);
                    Object[] param = { child };
                    if (mi != null)
                        mi.Invoke(element, param);
                }
                catch (Exception )
                {
                }
            }
            return element;
        }
        private void removeVisualChid(ref Object element, ref  Object child)
        {
            if (element != null)
            {
                try
                {
                    Type t = element.GetType();
                    System.Reflection.MethodInfo mi = t.GetMethod("RemoveVisualChild", BindingFlags.Instance | BindingFlags.NonPublic);
                    Object[] param = { child };
                    if (mi != null)
                        mi.Invoke(element, param);
                }
                catch (Exception )
                {
                }
            }
        }

    }
}
